<script setup>
import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
import { onHide, onLoad, onShow } from '@dcloudio/uni-app'

import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
import { ref } from 'vue'
import dayjs from 'dayjs'
import optionDeptPerson from '@/store/modules/optionDeptPerson'
import { addFinancialApplyControllerApi, selectOrderNoApi } from '@/api/attendance'
import { getUserProfile } from '@/api/system/user'
import approvalProcessState from '@/store/modules/approvalProcess'
import { isObjectValuesNotEmpty } from '@/utils/objIsnull.js'

const title = '业务招待报备申请'
const entertainDateRef = ref(null)
const businessHospitalityList = ref({
  businessCenter: '',
  businessCenterId: '',
  hospitalityPerson: '',
  hospitalityMatters: '',
  hospitalityBudget: '',
	entertainDate:'',
  escorts: [],
  orderId: ''
})

let attchment = ref([])
const getDesignDrawingsEmit = (emit) => {
  attchment.value = emit
}

const clickSave = async () => {
	console.log(businessHospitalityList.value,'0')
	if(isObjectValuesNotEmpty(businessHospitalityList.value)){
		let params = {
		  deptId: businessHospitalityList.value.businessCenterId,
		  orderId: businessHospitalityList.value.orderId,
		  date: dayjs(businessHospitalityList.value.entertainDate).format('YYYY-MM-DD hh:mm:ss'),
		  reason: businessHospitalityList.value.hospitalityMatters,
		  serviceObject: businessHospitalityList.value.hospitalityPerson,
		  money: businessHospitalityList.value.hospitalityBudget,
		  personnel: JSON.stringify(businessHospitalityList.value.escorts),
		  ruleId: 'yewu',
		  type: '业务招待报备申请',
		  attchment: JSON.stringify(attchment.value),
		  userId: userId.value,
		  approve: approvalProcessState.state.approvalProcess,
		}
		let { code } = await addFinancialApplyControllerApi(params)
		if (code === 200) {
		  uni.showToast({
		    title: '提交成功',
		    duration: 1500,
		    icon: 'success',
		  })
		  setTimeout(() => {
		    uni.navigateBack()
		  }, 1000)
		}
	}else{
		uni.showToast({
			title:'请输入完整数据',
			icon:'error'
		})
	}
 
}

const escortsFn = () => {
  uni.navigateTo({
    url: '/components/OptionDeptPerson/index?type=person',
  })
}
const userId = ref(null)

const formatter = (type, value) => {
  if (type === 'year') {
    return `${value}年`
  }
  if (type === 'month') {
    return `${value}月`
  }
  if (type === 'day') {
    return `${value}日`
  }
  if (type === 'hour') {
    return `${value}时`
  }
  if (type === 'minute') {
    return `${value}分`
  }
  return value
}

const entertainDateFn = () => {
  entertainDateRef.value.open()
}
let entertainDatePicker = dayjs()
const entertainDateConfirm = (obj) => {
  businessHospitalityList.value.entertainDate = dayjs(obj.value).format('YYYY-MM-DD')
}

const popupRef = ref(null)
const list1 = ref([{}])
const openPopupSelect = () => {
  popupRef.value.open()
}

// 单选弹窗选项点击事件
const itemClick = (item) => {
  if (item.item.label) {
    businessHospitalityList.value.businessCenter = item.item.label
    businessHospitalityList.value.businessCenterId = item.item.name
    businessHospitalityList.value.orderId = item.val

    popupRef.value.close()
  }
}

function debounce(func, wait) {
  let timeout
  return function (...args) {
    const context = this
    clearTimeout(timeout)
    timeout = setTimeout(() => func.apply(context, args), wait)
  }
}

let inputChangeSearchDebounce = debounce(async function (emit) {
  let params = {
    pageNum: 1,
    pageSize: 50,
    orderId: emit,
  }
  let { code, rows } = await selectOrderNoApi(params)
  // 搜第一个工单的数据
  if (code === 200) {
    list1.value = rows.map((v) => {
      return {
        label: v.orderId,
        name: v.deptId,
        value: v.orderId,
        disabled: v.true,
        dataArr: v,
      }
    })
  }
}, 800)
const searchData = ref(null)
const inputChangeSearch = (emit) => {
  if (emit) {
    searchData.value = emit
    inputChangeSearchDebounce(emit)
  } else {
    list1.value = [{}]
  }
}
const getUserProfileFn = async () => {
  let { code, data } = await getUserProfile()
  if (code === 200) {
    userId.value = data.userId
  }
}
onLoad(() => {
  getUserProfileFn()
})
const goHistoryView = () => {
  uni.navigateTo({
    url: '/components/TypeDefineModel?type=业务招待报备申请&apiName=财务',
  })
}

onShow(() => {
  // if (optionDeptPerson.state.optionPerson) {
  //   businessHospitalityList.value.escorts = optionDeptPerson.state.optionPerson.map((v) => {
  //     return {
  //       userId: v.userId,
  //       nickName: v.nickName,
  //     }
  //   })
  // }
	// 监听事件 获取选择的人员
	uni.$on('getOptionPerson', (data) => {
		// console.log(data, '人员data')
		businessHospitalityList.value.escorts = data.map((v) => {
			return {
				userId: v.userId,
				nickName: v.nickName,
			}
		})
	});
})

onHide(() => {
  // console.log(optionDeptPerson.mutations.clearOptionDept())
})
</script>

<template>
  <view>
    <HeaderNavTabarSvg :title="title">
      <image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
    </HeaderNavTabarSvg>

    <view class="picker-content" @tap="openPopupSelect">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">关联工单</text>
        <view class="info-list-right">
          <view v-if="businessHospitalityList.businessCenter" class="uni-input"
            >{{ businessHospitalityList.businessCenter }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <hy-popup-select
      ref="popupRef"
      v-model="businessHospitalityList.businessCenter"
      :filterKeys="['label', 'name']"
      :list="list1"
      @inputChange="inputChangeSearch"
      @itemClick="itemClick"
    >
      <template v-slot="{ item, index }">
        <view v-if="item?.dataArr?.customerName" class="card-list" style="margin-top: 12rpx">
          <view class="header-content">
            <text> {{ item.dataArr.customerName || '未知' }}</text>
            <text> {{ item.orderId || '未知' }}</text>
          </view>
          <view class="main-content">
            <h4>{{ item.dataArr.customerStore || '未知' }}</h4>
            <p>{{ item.dataArr.invoiceStatus || '未知' }}</p>
          </view>
          <view class="main-floor">
            <view class="floot-svg">
              <image src="@/static/svg/money.svg"></image>
              <text> {{ item.dataArr.totalOrderAmount || '未知' }}</text>
              <text>元</text>
            </view>
            <p class="floor-date">
              {{ dayjs(item.dataArr.orderDate).format('YYYY-MM-DD') || '未知' }}
            </p>
          </view>
        </view>
      </template>
    </hy-popup-select>

    <!--        <view class="picker-content" @tap="hospitalityPersonFn">-->
    <!--          <view class="measurement-info-list">-->
    <!--            <text class="info-list-left">经营中心</text>-->
    <!--            <view class="info-list-right">-->
    <!--              <view class="uni-input" v-if="businessHospitalityList.businessCenter">{{ businessHospitalityList.businessCenter }}</view>-->
    <!--              <view class="uni-inputs" v-else>请选择</view>-->
    <!--              <i class="iconfont icon-youjiantou"></i>-->
    <!--            </view>-->
    <!--          </view>-->
    <!--        </view>-->

    <view class="picker-content" @tap="entertainDateFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">招待日期</text>
        <view class="info-list-right">
          <view v-if="businessHospitalityList.entertainDate" class="uni-input"
            >{{ businessHospitalityList.entertainDate }}
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-datetime-picker
      ref="entertainDateRef"
      v-model="entertainDatePicker"
      :formatter="formatter"
      mode="date"
      @confirm="entertainDateConfirm"
    >
    </uv-datetime-picker>

    <view class="textarea-box">
      <text class="info-list-left isrequired">招待事由</text>
      <textarea
        v-model="businessHospitalityList.hospitalityMatters"
        class="textinfo-list"
        placeholder="请输入"
      ></textarea>
    </view>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">招待对象</text>
        <view class="info-list-right">
          <view class="info-content">
            <input
              v-model="businessHospitalityList.hospitalityPerson"
              adjust-position
              class="uni-input"
              placeholder="请输入"
              type="text"
            />
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">招待预算</text>
        <view class="info-list-right">
          <view class="info-content" style="display: flex; align-items: center">
            <input
              v-model="businessHospitalityList.hospitalityBudget"
              adjust-position
              class="uni-input"
              placeholder="请输入"
              type="text"
            />
            <p style="font-size: 28rpx; line-height: 1.4em">元</p>
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content" @tap="escortsFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">陪同人员</text>
        <view class="info-list-right">
          <view v-if="businessHospitalityList.escorts.length > 0" v-for="(item,index) in businessHospitalityList.escorts" :key="index" class="uni-input"
            >{{ item.nickName }} &nbsp
          </view>
          <view v-else class="uni-inputs">请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>

    <!--    说明附件-->
    <DescriptionAttachment @getDesignDrawingsEmit="getDesignDrawingsEmit" />
    <!--    审批流程-->
    <AuditApplicationSteps :money="businessHospitalityList.hospitalityBudget" :ruleId="'yewu'" />
    <!--    底部占位符-->
    <view class="blank"></view>
    <!--    保存-->
    <view class="buttonContainer">
      <button class="buttonClass" @click="clickSave">提交</button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import '@/static/style/index.scss';

.card-list {
  padding: 24rpx 32rpx;
  width: 686rpx;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  background-color: #ffffff;
  border-radius: 16rpx;
  border: 1rpx solid #e5e6eb;
  margin-bottom: 16rpx;

  .header-content,
  .main-content,
  .main-floor {
    @include flex($justify: space-between);
    color: #86909c;
    font-size: 24rpx;
    font-weight: 400;
  }

  .main-content {
    margin: 10rpx 0;

    h4 {
      color: #1d2129;
      font-size: 32rpx;
    }

    p {
      @include flex($justify: center, $align: center);
      padding: 0 8rpx;
      border-radius: 10rpx;
      min-width: 97rpx;
    }

    .待设计,
    .未下单,
    .待核算,
    .待上传安装图片,
    .待上传拆除照片 {
      color: #165dff;
      background-color: #e8f3ff;
    }

    .已核算,
    .已下单,
    .审核通过,
    .已通过 {
      color: #00b42a;
      background-color: #e8ffea;
    }

    .待审核 {
      color: #ff7d00;
      background-color: #fff7e8;
    }

    .驳回 {
      color: $btn-error-color;
      background-color: $btn-error-bg;
    }
  }

  .main-floor {
    .floot-svg {
      @include flex($align: center);

      image {
        width: 28rpx;
        height: 28rpx;
      }

      text {
        color: #1d2129;
      }
    }
  }

  .btn-install-order-list {
    margin-top: 20rpx;

    button {
      height: 56rpx;
      @include flex($justify: center, $align: center);
      border: none;
    }

    .btn-flex {
      @include flex($justify: space-between, $align: center);

      .接单 {
        background-color: #165dff;
        color: #fff;
      }

      button {
        width: 291rpx;
      }
    }

    .进行中 {
      background-color: #e8f3ff;
      color: #165dff;
    }

    .已拒接,
    .拒接 {
      background-color: #ffece8;
      color: #f53f3f;
    }

    .已完工 {
      background-color: #e8ffea;
      color: #00b42a;
    }

    .已超时 {
      background-color: #fff7e8;
      color: #ff7d00;
    }

    .row-btn {
      width: 100%;
    }
  }
}

.bukaSvg {
  width: 32rpx;
  height: 32rpx;
}

.buttonContainer {
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: center;
  width: 100%;
  align-items: center;
  padding: 16rpx;
  background-color: #fff;
}

.buttonClass {
  display: flex;
  width: 686rpx;
  height: 72rpx;
  justify-content: center;
  align-items: center;
  background: #165dff;
  border-radius: 16rpx;
  color: #fff;
  font-size: 32rpx;
  //margin-top: 40rpx;
  overflow: hidden;
}

.uni-inputs {
  color: #86909c !important;
}

.uni-input {
  text-align: right;
  font-size: 28rpx;
}

.blank {
  height: calc(72rpx + 32rpx + 20rpx);
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.bukabanci {
  .info-content {
    color: #86909c !important;
  }

  .info-list-left {
    // @include font-color-auxiliary;
    color: #86909c !important;
  }
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.popup-content {
  background: #f7f8fa;

  .header-box {
    padding: 30rpx 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    p {
      color: #1d2129;
      font-size: 34rpx;
      font-weight: 500;
    }

    .module-image-ba {
      border-radius: 32rpx;
      background: #f2f3f5;
      width: 45rpx;
      height: 45rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      image {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }
}

.measurement-info-list {
  @include flex($align: center, $justify: space-between);
  padding: $spacing-row-base;
  background: #fff;
  margin-top: 16rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .info-list-right {
    @include flex($align: center);

    .info-content {
      @include textStyleBase;
      font-size: 34rpx;
    }

    .iconfont {
      margin-left: 16rpx;
      // @include font-color-auxiliary;
      color: $uni-fc-fu-lin;
    }
  }

  .unit {
    // @include font-color-auxiliary;
    color: $uni-fc-fu-lin;
  }
}
</style>
